'use strict'

define([ 'jquery', 'bootstrap', 'select2', 'validate', 'validate.en',
		'koala-search', 'shiftform', 'modalSearch', 'datetimepicker',
		'datetimepickerCN' ], function($, validate) {

	return {

		init : function() {

			this.initPage();
			this.initWarehouse();
			this.initCompany();
			this.initClient();
			this.initVehicle();
			this.initManufacturer();
			this.initCancel();
			this.triggerModalSearch();

		},

		initPage : function() {

			$(document).find('form').bt_validate();

			$('select[name="organizationId"]').select2({
				theme : "classic",
				placeholder : '请选择',
				minimumResultsForSearch : -1,
				allowClear : true,
			});

			$('select[name="business"]').select2({
				theme : "classic",
				placeholder : '请选择',
				minimumResultsForSearch : -1,
				allowClear : true,
			});

			$('select[name="collectStatus"]').select2({
				theme : "classic",
				placeholder : '请选择',
				minimumResultsForSearch : -1,
				allowClear : true,
			});

			$('select[name="invoice"]').select2({
				theme : "classic",
				placeholder : '请选择',
				minimumResultsForSearch : -1,
				allowClear : true,
			});

			$('select[name="balanced"]').select2({
				theme : "classic",
				placeholder : '请选择',
				minimumResultsForSearch : -1,
				allowClear : true,
			});

			$('.date').datetimepicker({
				format : 'yyyy-mm-dd',
				autoclose : true,
				language : 'zh-CN',
				todayBtn : 'linked',
				startDate : '${today}',
				todayHighlight : true,
				pickerPosition : 'bottom-right',
				minView : 'month'
			});

			$(document).on('keyup', 'textarea[name=loadNumbers]', function() {
				var listVal = $(this).val();
				if (listVal != '') {
					var listSize = listVal.split('\n').length;
					$('span.loadNumbers-row-size').text(listSize);
					if (listSize > 999) {
						alert('最多999个参数');
					}
				}
			});

			$(document).on('keyup', 'textarea[name=orderCodes]', function() {
				var listVal = $(this).val();
				if (listVal != '') {
					var listSize = listVal.split('\n').length;
					$('span.orderCodes-row-size').text(listSize);
					if (listSize > 999) {
						alert('最多999个参数');
					}
				}
			});

		},

		initWarehouse : function() {

			$('[data-target="#modal-search-fromWarehouse"]').modalsearch({
				url : window.__CTX__.root + '/warehouses/query',
				tableColumns : [ '出发地代码', '出发地名称' ],
				tableValues : [ 'code', 'name' ],
				displayValues : [ 'code', 'name' ],
				values : [ 'id' ]
			});

			$('[data-target="#modal-search-toWarehouse"]').modalsearch({
				url : window.__CTX__.root + '/warehouses/query',
				tableColumns : [ '到达地代码', '到达地名称' ],
				tableValues : [ 'code', 'name' ],
				displayValues : [ 'code', 'name' ],
				values : [ 'id' ]
			});

			var options = {
				time : 1000,
				url : window.__CTX__.root + "/warehouses/query",
				queryName : "code",
				value : [ "id" ],
				display : [ "code", "name" ]
			}

			$('.fromWarehouse-search').koalaSearch(options);
			$('.toWarehouse-search').koalaSearch(options);

		},

		initCompany : function() {

			$('[data-target="#modal-search-consignor"]').modalsearch({
				url : window.__CTX__.root + '/transportation-companys/query',
				tableColumns : [ '运输公司代码', '运输公司名称' ],
				tableValues : [ 'code', 'name' ],
				displayValues : [ 'code', 'name' ],
				values : [ 'id' ]
			});

			$('[data-target="#modal-search-company"]').modalsearch({
				url : window.__CTX__.root + '/transportation-companys/query',
				tableColumns : [ '外借公司代码', '外借公司名称' ],
				tableValues : [ 'code', 'name' ],
				displayValues : [ 'code', 'name' ],
				values : [ 'id' ]
			});

			var options = {
				time : 1000,
				url : window.__CTX__.root + "/transportation-companys/query",
				queryName : "code",
				value : [ "id" ],
				display : [ "code", "name" ]
			}

			$('.consignor-search').koalaSearch(options);
			$('.company-search').koalaSearch(options);

		},

		initClient : function() {

			$('[data-target="#modal-search-customer"]').modalsearch({
				url : window.__CTX__.root + '/clients/query',
				tableColumns : [ '客户代码', '客户名称' ],
				tableValues : [ 'code', 'name' ],
				displayValues : [ 'code', 'name' ],
				values : [ 'id' ]
			});

			var options = {
				time : 1000,
				url : window.__CTX__.root + "/clients/query",
				queryName : "code",
				value : [ "id" ],
				display : [ "code", "name" ]
			}

			$('.customer-search').koalaSearch(options);

		},

		initVehicle : function() {

			$('[data-target="#modal-search-vehicle"]').modalsearch(
					{
						url : function() {
							var url = window.__CTX__.root + '/vehicles/query';
							return url + "?companyIds="
									+ $("[name='organizationId']").val();
						},
						tableColumns : [ '驳运车代码', '驳运车名称' ],
						tableValues : [ 'code', 'name' ],
						displayValues : [ 'code', 'name' ],
						values : [ 'id' ]
					});

			var options = {
				time : 1000,
				url : window.__CTX__.root + "/vehicles/query",
				queryName : "code",
				value : [ "id" ],
				display : [ "code", "name" ]
			}

			$('.vehicle-search').koalaSearch(options);

		},

		initManufacturer : function() {

			$('[data-target="#modal-search-manufacturer"]').modalsearch({
				url : window.__CTX__.root + '/manufacturers/query',
				tableColumns : [ '制造商代码', '制造商名称' ],
				tableValues : [ 'code', 'name' ],
				displayValues : [ 'code', 'name' ],
				values : [ 'id' ]
			});

			var options = {
				time : 1000,
				url : window.__CTX__.root + "/manufacturers/query",
				queryName : "code",
				value : [ "id" ],
				display : [ "code", "name" ]
			}

			$('.manufacturer-search').koalaSearch(options);

		},

		initCancel : function() {
			$('.btn-cancle').on('click', function(e) {
				$('.textarea').val('');
			});
		},

		triggerModalSearch : function() {
			$('.modal-search').each(function() {
				var value = $(this).val();
				console.info(value);
				if (value) {
					$(this).trigger('blur');
				}
			});
		}

	}
})